﻿#include "SettingData.h"
#include "ErrLog.h"
#include <QFile>
#include <QTextStream>

SettingData SettingData::instance;

SettingData::SettingData(void)
{
    filePath = ErrLog::GetCurrentWorkingDir();
    Load();
}

SettingData::~SettingData(void)
{ 
    if (!domDoc.isNull())
        domDoc.clear();
}


QString SettingData::GetDataString(const QString& pathName, const QString& defaultValue)
{
    if (!docElement.isNull())
    {
        QStringList path = pathName.split("/");

        QDomNodeList nodeList;
        QDomElement element = docElement;
        QString attributeName;
        for (int i = 0; i < path.size(); i++)
        {
            QString nodeName;
            if (i == path.size() - 1)
            {
                //Last one, may contain an attribute, such as: Devices/Device::Name
                QStringList pathAndAttribute = path[i].split("::");
                nodeName = pathAndAttribute[0];
                if (pathAndAttribute.size() > 1)
                    attributeName = pathAndAttribute[1];
            }
            else
                nodeName = path[i];
            int nodeIndex = 0;
            SpliteIndex(nodeName, nodeIndex);
            nodeList = element.elementsByTagName(nodeName);
            if (nodeList.isEmpty())
                return defaultValue;
            element = nodeList.at(nodeIndex).toElement();
        }

        if (attributeName.isNull() || attributeName.isEmpty())
            return element.text();
        else
            return element.attribute(attributeName);
    }
    return defaultValue;
}

void SettingData::SetDataString(const QString& pathName, const QString value)
{
    if (!docElement.isNull())
    {
        QStringList path = pathName.split("/");

        QDomNodeList nodeList;
        QDomElement element = docElement;
        QString attributeName;
        for (int i = 0; i < path.size(); i++)
        {
            QString nodeName;
            if (i == path.size() - 1)
            {
                //Last one, may contain an attribute, such as: Devices/Device::Name
                QStringList pathAndAttribute = path[i].split("::");
                nodeName = pathAndAttribute[0];
                if (pathAndAttribute.size() > 1)
                    attributeName = pathAndAttribute[1];
            }
            else
                nodeName = path[i];
            //nodeName may contain an index
            int nodeIndex = 0;
            SpliteIndex(nodeName, nodeIndex);
            nodeList = element.elementsByTagName(nodeName);
            if (nodeList.isEmpty())
            {
                //如果路径中的节点不存在，则需要新建节点。
                QDomElement newElement = domDoc.createElement(path[i]);
                element.appendChild(newElement);
                element = newElement;
            }
            else
                element = nodeList.at(nodeIndex).toElement();
        }

        if (attributeName.isNull() || attributeName.isEmpty())
        {
            //-----Note-----
            //The Qt XML considers the text inside the element as a child.
            //Weird, isn't it?
            element.firstChild().setNodeValue(value);
        }
        else
            element.setAttribute(attributeName, value);
    }
}

void SettingData::Load()
{
	//Load xml file into xml document
    QFile file(filePath + "/SettingData.xml");
    if (!file.open(QFile::ReadOnly))
    {
        LOGERR("SettingData::Load", "无法打开配置文件:" + file.fileName());
        return;
    }
    if (domDoc.setContent(&file))
        docElement = domDoc.documentElement();
    else
        LOGERR("SettingData::Load", "配置文件:" + file.fileName() + "内容存在错误，无法装载。");

    file.close();
}

void SettingData::Save()
{
	//Save to xml document
    QFile file(filePath + "/SettingData.xml");
    if (file.open(QFile::WriteOnly | QFile::Text))
    {
        QTextStream out(&file);
        domDoc.save(out, QDomNode::EncodingFromDocument);
        file.close();
    }
    else
        LOGERR("SettingData::Save", "无法打开配置文件:" + filePath);
}

